<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of InscripcionesGenericas
 *
 *  @export Modules
 */
class InscripcionesGenericas extends Module {

    public function index() {
        $tipo = $this->request->get("formId", "1");
        $Formulario = $this->model->Formularios->getFormulario($tipo);
        
        $this->assign("Formulario", $Formulario);
        $this->assign("Action", $this->route->action("guardar"));
//        $this->assign("Path", ACAWEB_MODULES_PATH . DS . $this->getName() . DS . "views" . DS . "controls" . DS);
        $this->display();
    }

    public function guardar() {
        Loader::using("models::entities::ValoresFormularios");
        Loader::using("models::entities::Suscritos");

        $results = $this->request->post("Values", array());
        $names = $this->request->post("Names", array());
        
        $Respuestas = array();
        $ToClient = array();

        foreach ($results as $key => $value) {
            $textValue = is_array($value) ? implode(", ", $value) : $value;

            $valor = new ValoresFormularios();
            $valor->setIdCampo($key);
            $valor->setRespuesta($textValue);

            $Respuestas[] = $valor;
            $ToClient[str_replace(" ", "", $names[$key])] = $textValue;
        }

        $suscrito = new Suscritos();
        $suscrito->setRespuestas($Respuestas);
        $suscrito->setInfo($_SERVER["REMOTE_ADDR"] . ":" . $_SERVER["REMOTE_PORT"] . " - " . $_SERVER["HTTP_USER_AGENT"]);

        $this->model->Formularios->responderFormulario($suscrito);

        $this->assign("ToClient", $ToClient);
        $this->assign("client", $this->request->post("destination"));
        $this->display();
    }

}

?>
